package com.ztf.infoEncrypt;

import org.springframework.util.StringUtils;

public class EncryptUtil {

    public static void main(String[] args) {
        EncryptUtil encrypt = new EncryptUtil();
        String  str = encrypt.protectedName("赵腾富");
        String  str2 = encrypt.protectedName("乔宇");
        String  str3 = encrypt.mobileEncrypt("18615915853");
        System.out.println(str);
        System.out.println(str2);
        System.out.println(str3);
    }

    /**
     * 姓名长度
     */
    Integer RESULT_NAME_LENGTH =2;

    // 手机号码前三后四脱敏
    public  String mobileEncrypt(String mobile) {
        if (StringUtils.isEmpty(mobile) || (mobile.length() != 11)) {
            return mobile;
        }
        return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }

    //身份证前三后四脱敏
    public  String idEncrypt(String id) {
        if (StringUtils.isEmpty(id) || (id.length() < 8)) {
            return id;
        }
        return id.replaceAll("(?<=\\w{3})\\w(?=\\w{4})", "*");
    }

    public  String protectedName(String userName){
        userName = userName.trim();
        char[] r = userName.toCharArray();
        String resultName = "";
        if(r.length == RESULT_NAME_LENGTH){
            resultName =  r[0]+"*";
        }
        if (r.length > RESULT_NAME_LENGTH) {
            String star = "";
            for (int i = 0; i < r.length-RESULT_NAME_LENGTH; i++) {
                star=star+"*";
            }
            resultName = r[0]+star+r[r.length-1];
        }
        return resultName;
    }
}
